package com.jincp.common.brvah.item_decoration;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.State;
import android.view.View;

import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.utils.UIUtils;

/**
 * Created by lengwei on 2017/11/6.
 *
 * @Description 时间轴
 */
public class TimeLineDecoration extends RecyclerView.ItemDecoration {

    private Context mContext;
    private int selectIndex;
    private int startSkipCount = 0;
    private int endSkipCount = 0;
    private int circleSize;//圆圈的半径
    private Paint mPaint;//画笔
    private Paint mLinePaint;//画笔
    private Paint mShadowPaint;//画笔
    private int lineSize = 4;//画笔宽度
    private int defaultColor;
    private int selectColor;
    private int offsetX;


    public TimeLineDecoration(Context context, int selectIndex, int startSkipCount, int endSkipCount) {
        this.mContext = context;
        this.selectIndex = selectIndex;
        this.startSkipCount = startSkipCount;
        this.endSkipCount = endSkipCount;
        circleSize = UIUtils.dp2px(6);
        defaultColor = Color.parseColor("#D8D8D8");
        selectColor = Color.parseColor("#F8426A");
        offsetX = UIUtils.dp2px(30);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(defaultColor);

        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStyle(Paint.Style.FILL);
        mLinePaint.setColor(defaultColor);

        mShadowPaint = new Paint();
        mShadowPaint.setStyle(Paint.Style.STROKE);
        mShadowPaint.setStrokeWidth(lineSize);
        mShadowPaint.setColor(Color.parseColor("#3FF8426A"));
        mShadowPaint.setAntiAlias(true);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
        int position = parent.getChildAdapterPosition(view);
        int count = parent.getAdapter().getItemCount();
        if (parent.getAdapter() instanceof BaseQuickAdapter ) {
            BaseQuickAdapter adapter = (BaseQuickAdapter) parent.getAdapter();
            count -= adapter.getHeaderLayoutCount();
        }
        if (!needSkip(position, count)) {
            Rect rect = new Rect(offsetX, 0, 0, 0);//使item从outRect中右移offsetX
            outRect.set(rect);
        }
    }

    @Override
    public void onDraw(Canvas canvas, RecyclerView parent, State state) {
        super.onDraw(canvas, parent, state);
        int childCount = parent.getChildCount();
        if (parent.getAdapter() instanceof BaseQuickAdapter ) {
            childCount -= ((BaseQuickAdapter)parent.getAdapter()).getHeaderLayoutCount();
        }
        for (int index = 0; index < childCount; index++) {
            if (needSkip(index, childCount)) {
                continue;
            }
            View child = parent.getChildAt(index);
            int left = child.getLeft();
            int top = child.getTop();
            int bottom = child.getBottom();
            int right = child.getRight();

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int bottomMargin = params.bottomMargin;//防止在item根布局中设置marginTop和marginBottom
            int topMargin = params.topMargin;

            int pointX = left / 2;
            int pointY = top - topMargin + UIUtils.dp2px(15);

            if (index-startSkipCount != 0) {
                drawTopLine(canvas, mLinePaint, index-startSkipCount <= selectIndex, pointX, pointY, top - topMargin);
            }
            drawBottomLine(canvas, mLinePaint, index < selectIndex + endSkipCount, pointX, pointY, bottom + bottomMargin);

            drawPoint(canvas, mPaint, index, selectIndex, pointX, pointY);
        }
    }

    private void drawTopLine(Canvas canvas, Paint mPaint, boolean isSelected, int pointX, int pointY, int top) {
        if (isSelected) {
            mPaint.setColor(selectColor);
        } else {
            mPaint.setColor(defaultColor);
        }
        canvas.drawLine(pointX, top, pointX, pointY - circleSize / 2, mPaint);
    }

    private void drawBottomLine(Canvas canvas, Paint mPaint, boolean isSelected, int pointX, int pointY, int bottom) {
        if (isSelected) {
            mPaint.setColor(selectColor);
        } else {
            mPaint.setColor(defaultColor);
        }
        canvas.drawLine(pointX, pointY + circleSize, pointX, bottom, mPaint);
    }


    private void drawPoint(Canvas canvas, Paint mPaint, int index, int selectIndex, int pointX, int pointY) {
        if (index <= selectIndex) {
            mPaint.setColor(selectColor);
        } else {
            mPaint.setColor(defaultColor);
        }
        if (index == selectIndex) {
            canvas.drawCircle(pointX, pointY + circleSize / 2, circleSize, mPaint);
            canvas.drawCircle(pointX, pointY + circleSize / 2, circleSize + 5, mShadowPaint);
        } else {
            canvas.drawCircle(pointX, pointY + circleSize / 2, circleSize, mPaint);
        }
    }

    public void setCircleSize(int circleSize) {
        this.circleSize = UIUtils.dp2px(circleSize);
    }

    public void setLineSize(int lineSize) {
        this.lineSize = lineSize;
    }

    public void setOffsetX(int offsetX) {
        this.offsetX = UIUtils.dp2px(offsetX);
    }

    private boolean needSkip(int position, int count) {
        return position < startSkipCount || position >= count - endSkipCount;
    }
}
